package cn.edu.zufe.mjt.bio;

import cn.edu.zufe.mjt.constans.InternetAddressConstants;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

/**
 * @author 蒙锦涛
 * @date 2025/6/26
 */


public class BioClient {
	public static void main(String[] args) throws InterruptedException {
		Thread tom = new Thread(() -> {
			try {
				sendHello();
			} catch (IOException | InterruptedException e) {
				throw new RuntimeException(e);
			}
		}, "Tom");
		Thread jerry = new Thread(() -> {
			try {
				sendHello();
			} catch (IOException | InterruptedException e) {
				throw new RuntimeException(e);
			}
		}, "Jerry");

		tom.start();
		jerry.start();
		tom.join();
		jerry.join();

	}

	private static void sendHello() throws IOException, InterruptedException {
		Socket socket = new Socket();
		socket.connect(new InetSocketAddress(InternetAddressConstants.HOST, InternetAddressConstants.PORT));
		OutputStream outputStream = socket.getOutputStream();
		// 发送十次问候
		for (int i = 0; i < 10; i++) {
			outputStream.write(("Hello" + Thread.currentThread().getName() + i).getBytes());
			outputStream.flush();
		}
		socket.close();
		Thread.sleep(2000);
	}
}
